Public MustInherit Class SQLParser
    Protected Friend MustOverride Sub ParseCommands(ByVal Commands() As String, ByRef tables As TableList)
    Protected MustOverride Function MyParserState() As SQLParserState

    Public Sub Parse(ByVal input As String, ByRef tables As TableList, ByVal project As BBXProject)
        Me._proj = project
        _ps = MyParserState()
        Dim cmd() As String = Me.GetCommands(input)
        If cmd Is Nothing Then Return
        If cmd.Length < 1 Then Return
        If cmd(0).ToLower() = "go" Then
            tables.SetNoActiveTable()
            _ps = SQLParserState.SQLParseNoTable
            Return
        End If
        ParseCommands(cmd, tables)
    End Sub

    Private _proj As BBXProject
    Private _ps As SQLParserState

    Protected Sub SetParserState(ByVal state As SQLParserState)
        Me._ps = state
    End Sub

    Public Function GetParserState() As SQLParserState
        Return _ps
    End Function

    Public Function GetParserProject() As BBXProject
        Return Me._proj
    End Function

    Protected Function SingleSpace(ByVal input As String) As String
        Dim procInput As String = input.Trim()
        If procInput Is Nothing Then Return Nothing
        If procInput.Length < 1 Then Return Nothing
        Dim out As String = ""
        Dim sCount As Integer = 0
        For i As Integer = 0 To procInput.Length - 1
            If Char.IsWhiteSpace(procInput.Chars(i)) Then
                sCount += 1
            Else
                sCount = 0
            End If
            If sCount <= 1 Then out = out & procInput.Chars(i)
        Next
        Return out
    End Function

    Protected Function GetCommands(ByVal input As String) As String()
        If input Is Nothing Then Return Nothing
        If input.Length < 1 Then Return Nothing
        Dim tmp As String = input.Replace("(", " ").Replace(")", " ").Replace("[", " ").Replace("]", " ").Replace(".", " ").Replace(" dbo ", " ").Replace(" DBO ", " ")
        tmp = Me.SingleSpace(tmp)
        If tmp Is Nothing Then Return Nothing
        Return tmp.Split(" ")
    End Function
End Class
